package cn.kgc.crowdfundingadminspringboot.controller;

import cn.kgc.crowdfundingadminspringboot.common.ResultEntity;
import cn.kgc.crowdfundingadminspringboot.pojo.Advertisement;
import cn.kgc.crowdfundingadminspringboot.service.AdvertisementService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/advertisement")
public class AdvertisementController {
@Resource
    AdvertisementService advertisementService;


    @RequestMapping("/to/page.html")
    public String toPage(){
        return "advertisement-page";
    }

    @ResponseBody
    @RequestMapping("/search/by/keyword")
    public ResultEntity<PageInfo<Advertisement>> search(
            @RequestParam(value="pageNum", defaultValue="1",required = false) Integer pageNum,
            @RequestParam(value="pageSize", defaultValue="5",required = false) Integer pageSize,
            @RequestParam(value="keyword", defaultValue="",required = false) String keyword
    ) {

        // 1.查询得到PageInfo对象
        PageInfo<Advertisement> pageInfo = advertisementService.queryForKeywordWithPage(pageNum, pageSize, keyword);

        // 2.封装结果对象返回
        return ResultEntity.successWithoutData(pageInfo);
    }

    @ResponseBody
    @RequestMapping("/get/list/by/id/list")
    public ResultEntity<List<Advertisement>> getRoleListByIdList(@RequestBody List<Integer> advertisementIdList) {
        List<Advertisement> advertisementList = advertisementService.getAdvertisementListByIdList(advertisementIdList);

        return ResultEntity.successWithoutData(advertisementList);
    }

    @ResponseBody
    @RequestMapping("/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> advertisementIdList) {

        advertisementService.batchRemove(advertisementIdList);

        return ResultEntity.successWithoutData();
    }

    @ResponseBody
    @RequestMapping("/save/role")
    public ResultEntity<String> saveRole(@RequestParam("name") String name,
                                         @RequestParam("iconpath") String iconpath,
                                         @RequestParam("status") Byte status,
                                         @RequestParam("url") String url,
                                         @RequestParam("userid") Byte userid) {

        advertisementService.saveAdvertisement(name,iconpath,status,url,userid);

        return ResultEntity.successWithoutData();
    }

    @ResponseBody
    @RequestMapping("/update/role")
    public ResultEntity<String> updateAdvertisement(Advertisement advertisement) {

        advertisementService.updateAdvertisement(advertisement);

        return ResultEntity.successWithoutData();
    }
}
